JavaScript is one of the most popular programming languages for web programming.
In this article, we’ll look at the basic syntax of modern JavaScript.
Numbers
The toFixed
method lets us round a number:
(3.14).toFixed(0); // returns 3
The toPrecision
method lets us round a number:
(3.14).`toPrecision`(1); // returns 3.1
The valueOf
method returns a number:
(3.14).valueOf();
The Number
function lets us convert anything to a number:
Number(true);
parseInt
converts non-numeric values to an integer:
parseInt("3 months");
parseFloat
converts non-numeric values to a floating-point number:
parseFloat("3.5 days");
The Number
function also comes with some constant properties.
They include:
Number.MAX_VALUE
— largest possible JS numberNumber.MIN_VALUE
— smallest possible JS numberNumber.NEGATIVE_INFINITY
— negative infinityNumber.POSITIVE_INFINITY
— positive infinity
Math
We can do various mathematical operations with the Math
object.
Math.round
rounds a number to an integer:
Math.round(4.1);
Math.pow
raises a base to an exponent:
Math.pow(2, 8)
Math.sqrt
takes the square root of a number:
Math.sqrt(49);
Math.abs
takes the absolute value of a number:
Math.abs(-3.14);
Math.ceil
takes the ceiling of a number:
Math.ceil(3.14);
Math.floor
takes the floor of a number:
Math.floor(3.14);
Math.sin
takes the sine of a number:
Math.sin(0);
Math.cos
takes the cosine of a number:
Math.cos(0);
Math.min
returns the minimum number in the list:
Math.min(1, 2, 3)
Math.max
returns the max number in the list:
Math.max(1, 2, 3)
Math.log
takes the natural log of a number:
Math.log(1);
Math.exp
raises e
to the given power:
Math.exp(1);
Math.random()
generates a number between 0 and 1 randomly:
Math.random();
We can generate any random number by using Math.floor
and Math.random
together:
Math.floor(Math.random() * 5) + 1;
5 is the max number and 1 is the min.
Global Functions
We can use the String
function to convert non-string values to strings:
String(23);
We can also call toString
on primitive values and objects to do the same:
(23).toString();
The Number
function lets us convert non-numbers to numbers:
Number("23");
decodeURI
unescapes URLs:
decodeURI(enc);
encodeURI
encodes URLs:
encodeURI(uri);
We can decode URI components with decodeURIComponent:
decodeURIComponent(enc);
And we can encode a string into a URI string with encodeURIComponent
:
encodeURIComponent(uri);
isFinite
lets us check whether a number is finite.
isNaN
lets us check whether a value is NaN
.
parseFloat
lets us parse a value into a floating-point number.
parseInt
lets us parse non-number values to integers.
Regex
JavaScript regex has the following modifiers:
i
— perform case-insensitive matchingg
— perform a global matchm
— perform multiline matching
And they can have the following patterns:
- “ — Escape character
d
— find a digits
— find a whitespace characterb
— find a match at the beginning or end of a wordn+
— contains at least one nn*
— contains zero or more occurrences of nn?
— contains zero or one occurrence of n^
— start of string$
— end of stringuxxxx
— find the Unicode character.
— Any single character(a|b)
— a or b(...)
— Group section[abc]
— In range (a, b or c)[0–9]
— any of the digits between the brackets[^abc]
— Not in ranges
— White spacea?
— Zero or one ofa
a*
— Zero or more ofa
a*?
— Zero or more, ungreedya+
— One or more ofa
a+?
— One or more, ungreedya{2}
— Exactly 2 ofa
a{2,}
— 2 or more ofa
a{,5}
— Up to 5 ofa
a{2,5}
— 2 to 5 ofa
a{2,5}?
— 2 to 5 ofa
, ungreedy[:punct:]
— Any punctuation symbol[:space:]
— Any space character[:blank:]
— Space or tab
Conclusion
JavaScript comes with many useful functions.
We can use regex to match patterns in strings.